var Note = function(_options){
	var defOptions = {
			id : "",
			content : "",
			x : 0,
			y : 0,
			width : "40px",
			height : "40px",
			editPosition : function(){return false;},
			editContent : function(id,curtext){
				var strs = id.split("-");
				param ={};
				param.id=strs[1];
				param.content=curtext;
				$.ajax({
					  async:'false',
					  type: "post",
					  url: "/ietm/commentTags/updateAnno",
					  data:param,
					  success:function(data){
						  if(data==='success'){
			    		    	 $.Zebra_Dialog('<div class="prompt"><div class="success-icon"></div><div class="success-title">修改成功!</div></div>', {
			    			            'title':'提示',
			    			            'buttons':['确定']
			    			         });
							  return true;
						  }
					  }	 
			});
					return true;
			},
			del : function(){return true;}
	};
	var options = $.extend(true, {}, defOptions, _options);
	
	var contentpX = contentX = px = pageX = options.x;
	var contentpY = contentY = py = pageY = options.y;
	var doc = $(document);
	var width = doc.width();
	var height = doc.height();
	var context = options.content;
	var margin = 5;
	var view = $('<img id="' + options.id + '" src="/images/icon_zs.png" alt="注释"></img>').css({
		zIndex : '500',
		position : 'absolute',
//		border : '1px solid #000000',
//		background : 'url(../images/icon_zs.png)',//"#FFFF00",
//		width : options.width,
//		height : options.height
	}).hide();
	var areaView = $('<div id="'+ options.id +'-area"></div>').css({
		position : 'absolute',
		zIndex : '550',
		border : '1px solid #FFD700'     //金色
	}).hide();
//	.append('<div class="'+options.id + '-del"><button id="content-del-' + options.id + '" title="删除注释" class="button-del" type="button">X</button></div>').hide();
	
	var content = $('<textarea rows="4" cols="20" id="' + options.id + '-content">' + context + '</textarea>')
	.css({
		border : '1px solid #FFD700',
		backgroundColor:'#FFFACD',     //柠檬沙色
		overflow : 'auto'
	});
	content.on('change',function(event){
		var curtext = $(event.target).val();
		if(!curtext || curtext.length == 0){
			alert("注释内容不允许为空");
			$(event.target).val(context);
		}else {
			if(context == curtext){
				return ;
			}else{
				if(options.editContent(options.id,curtext)){
					context = curtext;
					return;
				}else{
					alert("注释内容修改失败");
					$(event.target).val(context);
				}
			}
		}
		
	});
	areaView.append(content);
	$('#content').append(view);
	$('#content').append(areaView);
	$('.' + options.id + '-del').css({
		width:'100%',
		height : '22px',
		borderBottom : '2px solid #dddddd',
		backgroundColor : '#e9e9e9'
	});
	
	var button = $('button#content-del-' + options.id).css({
		float : 'right',
		width : '20px',
		margin : '1px 5px 1px 0px ',
		lineHeight : '14px',
		padding : 	'0px'
		/*
		height : '18px',
		fontSize : '8pt',
		*/
	});
	button.on('click',function(){
		if(confirm("确定删除注释")){
			if(options.del(options.id)){
				view.remove();
				areaView.remove();
			}else{
				alert("删除失败");
			}
		}
	});
	
	view.on('mousedown',function(){
		var position = areaPosition(view.position().left,view.position().top);
		contentpX = position.x;
		contentpY = position.y;
		areaView.css({
			left : contentpX,
			top : contentpY
		});
		areaView.toggle();
	});
	view.Tdrag({
		cbStart : function(){
			pageX = view.position().left;
			pageY = view.position().top;
			contentX = areaView.position().left;
			contentY = areaView.position().top;
		},//移动前的回调函数
	   cbMove:function(){
		   var position = areaPosition(view.position().left,view.position().top);
		   contentpX = position.x;
		   contentpY = position.y;
		   areaView.css({
				left : contentpX,
				top : contentpY
		   });
	   },//移动中的回调函数
	    cbEnd : function(){
	    	if(view.position().left === px && view.position().top === py){
	    		return;
	    	}else{
	    		if(options.editPosition(options.id,view.position())){
	    			px = view.position().left;
		    		py = view.position().top;
	    		}else{
	    			view.css({
	    				left : pageX,
	    				top : pageY
	    			});
	    			areaView.css({
	    				left : contentX,
	    				top : contentY
	    			});
	    		}
	    	}
	    }//移动结束时候的回调函数
	});
	function areaPosition(left,top){
		 var x = left + view.width() + margin;
		 var y = top ;
		 var position = {};
		 position['x'] = x;
		 position['y'] = y;
		 if(x + areaView.width() >= width - margin){
			 position['x'] = x - view.width() - areaView.width() - 2 * margin;
		 }
		 if(y + areaView.height() >= height - margin){
			 position['y'] = y - areaView.height() + view.width();
		 }
		 return position;
	}
	if (pageX + view.width() >= width) {
		pageX = width - view.width();
	}
/*	if (pageY + view.height() >= height) {
		pageY = height - view.height();
	}*/
	view.css({
		left : pageX,
		top : pageY
	}).show();
	return {
		moveBack : function(){
			px = pageX;
			py = pageY;
			view.css({
				left : pageX,
				top : pageY
			});
			areaView.css({
				left : contentX,
				top : contentY
			});
		}
	};
};

var NoteForm = function(_options) {
	var defOptions = {
		id : "ui-note-view",
		x : 0,
		y : 0,
		subTitle : "",
		version : "",
		publishDate : "",
		title : "",
		userName : "",
		content : "",
		visibility : ""
	};
	var options = $.extend(true, {}, defOptions, _options);
	function remove(){
		$('#' + options.id).remove();
	}
	var pageX = options.x;
	var pageY = options.y;
	var doc = $(document);
	var width = doc.width();
	var height = doc.height();
	var margin = 5;
	remove();
	var noteView = $('<div id="' + options.id + '"></div>').css({
		padding : "0.2em",
		position : 'absolute',
		zIndex : '9999',
		border : '1px solid #C5C5C5',
		color : '#333333'
	}).hide();
	// style="vertical-align:top; text-align: right;"
	// style="border: 1px solid #dddddd; overflow: auto;"
	var fieldView = $('<fieldset id="ui-note-set"><legend>添加注释</legend></fieldset>');
	fieldView.appendTo(noteView);
	
	var formView = $('<form id="ui-note-form"  enctype="multipart/form-data"></form>');
	formView.appendTo(fieldView);
	
	if (Object.prototype.toString.call(options.params) === '[object Array]' && options.params.length > 0) {
		$.each(options.params,function(index,param){
			formView.append('<input type="hidden" name="' + param.name + '" value="' + param.value + '" \>');
		});
		
	}
	
/*	var tableView = $('<table class="ui-note-table"></table>')
	.append('<tr><td><label>数据模块编码：</label></td><td>'+ options.subTitle + '</td></tr>')
	.append('<tr><td><label>版本：</label></td><td>' + options.version + '</td></tr>')
	.append('<tr><td><label>发布日期：</label></td><td>' + options.publisDate + '</td></tr>')
	.append('<tr><td><label>标题：</label></td><td><input name="title" type="text"></td></tr>')
	.append('<tr><td><label >添加人：</label></td><td>'+ options.userName + '</td></tr>')
	.append('<tr><td><label>内容：</label></td><td><textarea name="content" rows="6" cols="30"></textarea></td></tr>')
	.append('<tr><td><label>可见性：</label></td><td><select name="visibility"><option value="0">共享</option><option value="1">私有</option></select></td></tr>')
	.append('<tr><td></td><td class="ui-note-t-item"><input name="file" type="file"></td></tr>');
	
	tableView.appendTo(formView);*/
	
	
	var tableView = $('<table class="ui-note-table" id="note-data"></table>')
	.append('<tr><td><label>数据模块编码：</label></td><td>'+ options.subTitle + '</td></tr>')
	.append('<tr><td><label>版本：</label></td><td>' + options.version + '</td></tr>')
	.append('<tr><td><label>发布日期：</label></td><td>' + options.publishDate + '</td></tr>')
	.append('<tr><td><label>标题：</label></td><td><input name="title" type="text"></td></tr>')
	.append('<tr><td><label >添加人：</label></td><td>'+ options.userName + '</td></tr>')
	.append('<tr><td><label>内容：</label></td><td><textarea name="content" rows="6" cols="30"></textarea></td></tr>')
	.append('<tr><td><label>可见性：</label></td><td><select name="visibility"><option value="0">共享</option><option value="1">私有</option></select></td></tr>')
	//.append('<tr><td></td><td class="ui-note-t-item"><input type="text" name="file" class="input-pub" /><input type="button" name="upload2" value="上传" onClick="this.form.file1.click();"  /><input name="file1" type="file" style="display:none;" onChange="this.form.file.value=this.value;"></td></tr>');
	//.append('<tr><td></td><td class="ui-note-t-item"><input type="file" name="file" /></td></tr>');
	tableView.appendTo(formView);
	
	formView.append('<div class="ui-note-foot"><div><input id="ui-note-save" type="submit" value="保存"/><button id="ui-note-cancel" type="button">取消</button></div></div>');
	
	$("body").append(noteView);
	noteView.on("mousedown",function(e){
		if(3 === e.which){
			return false;
		}
	});
	$('#ui-note-cancel').on('click', function(){
		remove();
	});
	$('.ui-note-table>tr>td:first-child').css({
		verticalAlign : 'top',
		textAlign : 'right'
	});
	$('.ui-note-table textarea').css({
		border : '1px solid #dddddd',
		overflow : 'auto'
	}).change(function(){
		if(!$(this).val()){
			$(this).css({
				borderColor : '#EA5A70'
			}).attr('title','内容不能为空');
		}else{
			$(this).css({
				borderColor : '#dddddd'
			}).attr('title','');
		}
	});
	$('.ui-note-table input').css({
		border : '1px solid #dddddd',
		width:'256px',
		height : '20px'
	}).change(function(){
		if(!$(this).val()){
			$(this).css({
				borderColor : '#EA5A70'
			}).attr('title','标题内容不能为空');
		}else{
			$(this).css({
				borderColor : '#dddddd'
			}).attr('title','');
		}
	});
	$('.ui-note-table select').css({
		width:'80px',
		height : '20px'
	});
	$('div.ui-note-foot').css({
		textAlign :'left',
		marginTop:'0.5em',
		padding:'0.3em 1em 0.5em 0.4em',
		borderTop : '1px solid #dddddd'
	});
	$('div.ui-note-foot div').css({
		float : 'right'
	});
	$('div.ui-note-foot div button').css({
		margin : '.5em .4em .5em 0',
		cursor : 'pointer',
		border : '1px solid #c5c5c5',
		background : '#f6f6f6',
		padding : '.4em 1em',
	    display : 'inline-block',
	    position: 'relative',
	    lineHeight: 'normal',
	    verticalAlign: 'middle',
		textAlign: 'center',
		boxSizing: 'border-box'
	});
	if (pageX + noteView.width() >= width) {
		pageX = pageX - noteView.width() - margin;
	}
	if (pageY + noteView.height() >= height) {
		pageY = pageY - noteView.height() - margin;
	}
	noteView.css({
		'left' : pageX,
		'top' : pageY,
		background :'#FFFFFF'
	}).show();
	return {
		submit : function(_opt){
			formView.ajaxForm({
				url : _opt.url,
				dataType : _opt.dataType,
				data : _opt.data,
				type : _opt.type,
				beforeSubmit : function(formData, jqForm){
					var form = jqForm[0]; 
					var flag = true;
					if (!form.title.value ) {
						$(form.title).css({
							borderColor : '#EA5A70'
						}).attr('title','标题内容不能为空');
						flag = false; 
					} 
					if(!form.content.value){
						$(form.content).css({
							borderColor : '#EA5A70'
						}).attr('title','内容不能为空');
						flag = false;
					}
					return flag;
				},
				success : function(responseText, statusText){
					if($.isFunction(_opt.success)){
						if(_opt.success($('#ui-note-form')[0],responseText)){
							remove();
						}
					}else{
						remove();
					}
				}
			});
		}
	};

};